/*
 * @Author: YeFei
 * @Date: 2021-11-08 10:12:16
 * @LastEditTime: 2021-11-09 12:12:29
 * @Descripttion:
 */
import http from "./http"
import Viewer from "v-viewer"
import token from "./token"
import day from "./day"
import myfunc from "./func"
import message from "./message"
import md5 from "js-md5"
import debounce from "lodash/debounce"
import excelUtil from "./excelUtil"
import power from "./power"
import e from "./e"
import {
    isEmpty,
    formatDate,
    removeField,
    notempty,
    standardDateToTimstamp,
    chooseFields,
} from "./utils"

export default {
    install(Vue) {
        Vue.prototype.$debug = process.env.NODE_ENV === "development"
        Vue.prototype.$day = day
        Vue.prototype.$func = myfunc
        Vue.prototype.$md5 = md5
        Vue.prototype.$http = http
        Vue.prototype.$viewer = Viewer
        Vue.prototype.$token = token
        Vue.prototype.$message = message
        Vue.prototype.$debounce = debounce
        Vue.prototype.$excelUtil = excelUtil
        Vue.prototype.$power = power
        Vue.prototype.$e = e
        Vue.prototype.$isEmpty = isEmpty
        Vue.prototype.$formatDate = formatDate
        Vue.prototype.$removeField = removeField
        Vue.prototype.$notempty = notempty

        Vue.prototype.$standardDateToTimstamp = standardDateToTimstamp
        Vue.prototype.$chooseFields = chooseFields

        Vue.prototype.$initEdit = (that, item, data) => {
            myfunc.initEdit(that, item, data)
        }
        Vue.prototype.$initItem = (that, item, keyArr, data) => {
            myfunc.initItem(that, item, keyArr, data)
        }
        let roleMap = {}
        const modulesFiles = require.context("../api", true, /.js$/)
        modulesFiles.keys().reduce((modules, modulePath) => {
            // // 忽略第1个js
            const moduleName = modulePath.replace(/^.\/(.*)\.\w+$/, "$1")
                // // 执行modulesFiles函数，返回一个对象{default: {// 文件内容}, _esModule: true}
            const value = modulesFiles(modulePath)
                // console.log(moduleName, value)
                // modules[moduleName] = value.default
                // return modules
            for (let key in value) {
                if (!value[key] || !value[key].role) continue
                roleMap[moduleName] = value[key].role
            }
        }, {})
        console.log(roleMap)
        Vue.directive("has", {
            inserted(el, binding) {
                // console.log(el, binding)
                const value = binding.value
                let flag = false
                if (Object.prototype.toString.call(value) == "[object String]") {
                    let tmp = value.split(".")
                    if (
                        tmp.length == 2 &&
                        tmp[0] &&
                        tmp[1] &&
                        roleMap[tmp[0]] &&
                        roleMap[tmp[0]][tmp[1]]
                    ) {
                        if (power.check(roleMap[tmp[0]][tmp[1]])) {
                            flag = true
                        }
                    }
                }
                el.style.display = flag ? "" : "none"
            },
        })
    },
}